<?php

/**
 * @file
 * This handles interacting with the node table for the site.
 */

 class node {
 	 public $nid = 0;
   public $title = '';
   public $uid = 1;
   public $eid = 1;
   public $updated_at;
  
   function node($Nid = NULL,$Title = NULL,$User_id = NULL,$Entity_eid = NULL,$Updated_at = NULL) {
     $this->nid        = $Nid;
     $this->title      = $Title;
     $this->uid        = $User_id;
     $this->eid        = $Entity_eid;
     if (!$Updated_at) {
       $Updated_at = database_date(date());
     }
   }
   
 	/**
	 * Get a subset of nodes.
   * 
   * @param $id = NULL
   *   if NULL, this will get the latest one, otherwise this will get the nodes
   *     with the indexes in this array.
	 */
 	public static function get($id = NULL) {
 	  global $conn;
    if (empty($conn)) {
      database_connection();
    }
 	  $query = '';
 	  // Confirm the parameter is set
 	  if($id) {
 	    // If the parameter is an integer, then select a single.
 	    if (is_numeric($id) && is_int($id+0)) {
 	      $query = 'SELECT n.`nid`,n.`updated_at`,n.`title`,n.`user_uid`,n.`entity_eid` ';
        $query .= 'FROM `NODES` n ';
        $query .= 'WHERE n.nid=?';
        $statement = $conn->prepare($query);
        $statement::bind_param('i',$id);
        $statement->execute();
        $statement->bind_result($this->nid, $this->updated_at, $this->title, $this->user_uid, $this->entity_eid);
        if ($return = $statement->fetch()) {
          return $this;
        }
        return $return;
 	    }
      // else if it is a non empty array, then select multiple.
      else if (is_array($id) && !empty($id)) {
        $query = 'SELECT n.`nid`,n.`updated_at`,n.`title`,n.`user_uid`,n.`entity_eid`';
        $query .= 'FROM `NODES` n';
        $query .= 'WHERE n.nid IN (';
        foreach ($id as $item) {
          $query .= "?,";
        }
        $query = substr($query,0,-1) . ')';
        $statement = $conn->prepare($query);
        foreach($id as $item) {
          $statement::bind_param('i',$id);
        }
        $statement->execute();
        $statement->bind_result($nid, $updated_at, $title, $user_uid, $entity_eid);
        $return = array();
        while ($statement->fetch()) {
          $n = new node($nid, $updated_at,$title,$user_uid,$entity_eid);
          $return[] = $n;
        }
        return $return;
      }
 	  }
    // If no query was constructed above, get the most recent node.
    if (empty($query)) {
      $query = 'SELECT n.`nid`,n.`updated_at`,n.`title`,n.`user_uid`,n.`entity_eid`';
      $query .= 'FROM `NODES` n';
      $query .= 'ORDER BY n.`updated_at` DESC';
      $query .= 'LIMIT 0,1';
    } 
    $statement = $conn->prepare($query);
    $statement->execute();
    $statement->bind_result($this->nid, $this->updated_at, $this->title, $this->user_uid, $this->entity_eid);
    if ($return = $statement->fetch()) {
      return $this;
    }
    return $return;
 	}
	
	/**
	 * Get all the nodes.
   * 
   * @param $order_by = 'nid'
   *  The field to sort the results by.
   * @param $sequence = 'ASC'
   *  Whether to sort in ascending or descending order.
   * @return array of nodes
	 */
	public static function getAll($order_by = 'nid',$sequence = 'ASC') {
	  global $conn;
    if (empty($conn)) {
      database_connection();
    }
		$return = array();
    $query = 'SELECT n.`nid`,n.`updated_at`,n.`title`,n.`user_uid`,n.$entity_eid';
    $query .= 'FROM NODES n';
    $possible = array('ASC','DEC');
    if ($order_by && in_array($sequence,$possible)) {
      $query .= "ORDER BY '$order_by' $sequence";
    }
    $statement = $conn->prepare($query);
    $statement->execute();
    $statement->bind_result($nid, $updated_at, $title, $user_uid, $entity_eid);
    while ($statement->fetch()) {
      $return[] = new node($nid,$updated_at,$title,$user_uid,$entity_eid);
    }
    return $return;
	}
	
   /**
    * Insert a new node and its entity as well.
    * 
    * @param $title
    *  The title of the node to insert.
    * @param $uid
    *  The user_id of the author of this node.
    * @param $path = NULL
    *  The path to view this node.
    * @return boolean
    *  TRUE if successful, FALSE if failure.
    */
	public static function insert($title, $uid, $path = NULL) {
	  if ($title) {
		$this->title = $title;
	  }
	  if ($uid) {
	  	$this->uid = $uid;
	  }
	  $query = 'INSERT INTO `ENTITIES` (definition_id,path) ';
	  $query .= 'VALUES (?,?)';
	  $statement1 = $conn->prepare($query);
	  $statement1->bind_param('is', 7, $path);
	  $query = 'INSERT INTO `NODES` (`title`,`user_uid`,`entity_eid`)';
	  $query .= "VALUES (?, ?, MYSQL_INSERT_ID())";
	  $statement2 = $conn->prepare($query);
	  $statement2->bind_param('si', $title, $uid);
	  if ($statement1->execute()) {
	    return $statement2->execute();
	  }
	  return FALSE;
	}
	/**
	 * Update an existing nodes database entry to current objects values.
	 */
	public static function update() {
	  global $conn;
    if (empty($conn)) {
      database_connection();
    }
		$query = 'UPDATE NODES n';
		$query .= 'SET n.`updated_at`=?,n.`title`=?,n.`user_uid`=?,n.`entity_eid`=?';
    $query .= 'WHERE n.`nid`=?';
    $statement = $conn->prepare($query);
    $statement->bind_param('ssiii', $this->updated_at, $this->title, $this->uid,$this->eid,$this->nid);
    return $statement->execute();
	}
	
 }